//
//  WYReportFotterCell.m
//  Boss
//
//  Created by admin on 2018/6/9.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYReportFotterCell.h"
#import "WYAddImageViewCell.h"
#import <KSPhotoBrowser/KSPhotoBrowser.h>
@interface WYReportFotterCell()<UICollectionViewDelegate,UICollectionViewDataSource,KSPhotoBrowserDelegate>

@property (nonatomic,strong)UICollectionView *collectionView;

@end
@implementation WYReportFotterCell
-(void)setDataArray:(NSMutableArray *)dataArray
{
    _dataArray = dataArray;
    [self.collectionView reloadData];
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self createrSubViews];
    }
    return self;
}
-(void)createrSubViews
{
    WEAKSELF
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    layout.itemSize=CGSizeMake((kScreenWidth-40)/3, (kScreenWidth-40)/3);
    //横向最小距离
    layout.minimumInteritemSpacing=0.0f;
    layout.minimumLineSpacing=10.0f;
    //cell距离上  左  下 右 的距离
    layout.sectionInset=UIEdgeInsetsMake(10, 10, 10, 10);
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    UICollectionView * collectionView= [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:layout];
    // [collectionView setCollectionViewLayout:layout];
    [self addSubview:collectionView];
    
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(130);
        
    }];
    self.collectionView = collectionView;
    [collectionView registerNib:[UINib nibWithNibName:@"WYAddImageViewCell" bundle:nil] forCellWithReuseIdentifier:@"WYAddImageViewCell"];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    
    
    
    UIButton *submitBtn = [[UIButton alloc]init];
    [self addSubview:submitBtn];
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(50);
        make.top.equalTo(collectionView.mas_bottom).offset(10);
        make.bottom.mas_equalTo(-50);
    }];
    
    [submitBtn setBackgroundColor:kColorMainColor];
    [submitBtn setTintColor:[UIColor whiteColor]];
    [submitBtn setTitle:@"提交" forState:UIControlStateNormal];
    submitBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [submitBtn addTarget:self action:@selector(jubaoAction) forControlEvents:(UIControlEventTouchUpInside)];
    [submitBtn setCornerRadius:5.f];
    
    
    
    
}
-(void)jubaoAction
{
    if ([self.delegate respondsToSelector:@selector(didJuBaoAction)]) {
        [self.delegate didJuBaoAction];
    }
    
    
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.dataArray.count>=3) {
        return 3;
    }else{
        return self.dataArray.count+1;
    }
    
    
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WYAddImageViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WYAddImageViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"WYAddImageViewCell" owner:nil options:nil].firstObject;
    }
    if (self.dataArray.count>=9) {
        cell.deleteBtn.hidden = NO;
        cell.addImageView.image =self.dataArray[indexPath.row];
    }else{
        if (indexPath.row == self.dataArray.count) {
            cell.deleteBtn.hidden = YES;
            cell.addImageView.image =[UIImage imageNamed:@"image_add_icon"];
        }else{
            cell.deleteBtn.hidden = NO;
            cell.addImageView.image =self.dataArray[indexPath.row];
        }
        
    }
    cell.deleteBtn.tag = indexPath.row;
    [cell.deleteBtn addTarget:self action:@selector(deleteBtnClik:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
- (void)deleteBtnClik:(UIButton *)sender {
    

    if ([self.delegate respondsToSelector:@selector(deleteImageClick:)]) {
        [self.delegate deleteImageClick:sender.tag];
    }
    
    
//    if ([self collectionView:self.collectionView numberOfItemsInSection:0] <= self.dataArray.count) {
//        [self.dataArray removeObjectAtIndex:sender.tag];
////        [self.dataArray removeObjectAtIndex:sender.tag];
//        [self.collectionView reloadData];
//        return;
//    }
//
//
//     [self.dataArray removeObjectAtIndex:sender.tag];
//    [_collectionView performBatchUpdates:^{
//        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:sender.tag inSection:0];
//        [self->_collectionView deleteItemsAtIndexPaths:@[indexPath]];
//    } completion:^(BOOL finished) {
//        [self->_collectionView reloadData];
//    }];
////
   // [self reloadCell];
}





-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == self.dataArray.count) {
        //        [self.dataArray insertObject:@"example" atIndex:self.dataArray.count - 1];
        if ([self.delegate respondsToSelector:@selector(didSelectCollectionViewWithIndex:)]) {
            [self.delegate didSelectCollectionViewWithIndex:indexPath];
        }
    }else{
        NSMutableArray *items = @[].mutableCopy;
        for (int i = 0; i < self.dataArray.count; i++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            WYAddImageViewCell *cell =( WYAddImageViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
            UIImageView *imgV = cell.addImageView;
            UIImage *image =self.dataArray[i];
            KSPhotoItem *item = [KSPhotoItem itemWithSourceView:imgV image:image];
            [items addObject:item];
        }
        KSPhotoBrowser *browser = [KSPhotoBrowser browserWithPhotoItems:items selectedIndex:indexPath.row];
        browser.delegate = self;
        browser.dismissalStyle = KSPhotoBrowserInteractiveDismissalStyleRotation;
        browser.backgroundStyle = KSPhotoBrowserBackgroundStyleBlurPhoto;
        browser.loadingStyle = KSPhotoBrowserImageLoadingStyleDeterminate;
        browser.pageindicatorStyle = KSPhotoBrowserPageIndicatorStyleDot;
        browser.bounces = NO;
        [browser showFromViewController:self.selfVc];
    }
}






- (void)ks_photoBrowser:(nonnull KSPhotoBrowser *)browser didSelectItem:(nonnull KSPhotoItem *)item atIndex:(NSUInteger)index {
    
}
@end
